<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <!-- 选项 -->
      <el-checkbox
        v-for="role in roleList"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserId } from '@/api/user'
import { assignRole } from '@/api/emploees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
    // 用户的id 用来查询当前用户的角色信息
    // userId: {
    //   type: String,
    //   default: null
    // }
  },
  data() {
    return {
      roleList: [],
      checkList: [],
      roleIds: [],
      userId: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    //   获取角色列表
    async getRoleList() {
      const { rows } = await getRoleList()
      this.roleList = rows
    },
    // 根据id获取角色详情
    async getUserId(id) {
      this.userId = id
      const { roleIds } = await getUserId(id)
      this.checkList = roleIds
    //   console.log(roleIds)
    },

    // 点击确定
    async btnOk() {
      await assignRole({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('修改成功')
      this.$emit('update:showRoleDialog', false)
    },
    // 点击取消
    btnCancel() {
      this.checkList = []
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

